package com.zslibrary.Protocol;

import com.zslibrary.Enum.PipeResultDefine;
import com.zslibrary.Enum.Protocol;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Structure.GeneralRequestStruct;
import com.zslibrary.Structure.MessageStruct;
import com.zslibrary.Structure.SensorValueStruct;
import com.zslibrary.Tools.FrameBuilder;

public class GetSensorValue {
    private SensorValueStruct mSensorValue=null;

    public @StateEventDefine.StateEvent int getSensorValue(final Pipe pipe, int componentId, int sensor){
        GeneralRequestStruct request=new GeneralRequestStruct();
        FrameBuilder builder=new FrameBuilder(4);
        builder.append((short)componentId);
        builder.append((short)sensor);
        request.requstId= Protocol.MSG_REQUEST_SENSOR_VALUE;
        request.dataLength=4;
        request.data=builder.toBytes();
        final Pipe.HandleMessageCallback callback=new Pipe.HandleMessageCallback() {
            @Override
            public Boolean handle(MessageStruct[] messages) {
                for(MessageStruct message:messages){
                    switch (message.messageId){
                        case Protocol.MSG_SET_COMP_ERR:{
                            CommonMessage.processSetError(message);
                        }break;

                        case Protocol.MSG_CLEAR_COMP_ERR:{
                            CommonMessage.processClrError(message);
                        }break;

                        case Protocol.MSG_BOARD_RESET:{
                            pipe.interruptAsynchonousPipe();    /*可被终止*/
                        }break;

                        case Protocol.MSG_GET_SENSOR_VALUE:{
                            FrameBuilder msgBuilder=new FrameBuilder(message.messageData);
                            SensorValueStruct value=new SensorValueStruct();
                            value.componentId=(short)msgBuilder.popData(2);
                            value.sensorIndex=(short)msgBuilder.popData(2);
                            value.value=msgBuilder.popData(4);
                            mSensorValue=value;
                        }break;

                        default:
                            break;
                    }
                }
                return false;
            }
        };

        mSensorValue=null;
        @PipeResultDefine.PipeResult int result=pipe.asynchronousPipe(request,callback,2000);
        if(result==PipeResultDefine.ASYNC_ERROR)
            return StateEventDefine.EVT_DISCONNECT;
        else if(result==PipeResultDefine.ASYNC_INTERRUPT)
            return StateEventDefine.EVT_RESET;
        else if(result==PipeResultDefine.ASYNC_BUSY)
            return StateEventDefine.EVT_EXECUTE;
        else if(result==PipeResultDefine.ASYNC_OK)
            return StateEventDefine.EVT_NONE;
        return StateEventDefine.EVT_NONE;
    }

    public int getSensorValue(){
        if(mSensorValue==null)
            return -1;
        return mSensorValue.value;
    }
}
